package com.qfedu.service.Impl;

import com.qfedu.dao.RoomDao;

import com.qfedu.entity.Room;
import com.qfedu.entity.User;
import com.qfedu.service.ImgService;
import com.qfedu.service.RoomService;
import com.qfedu.service.UserService;

import com.qfedu.util.JsonTools;
import com.qfedu.utils.RoomUtils;

import com.qfedu.vo.ResultVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoomServiceImpl implements RoomService {
    @Resource
    private RoomDao roomDao;


    @Resource
    private UserService userService;


    @Resource
    private ImgService imgService;
    /**
     * 根据房源ID查询 房源信息
     * @param rid
     * @return
     */
    @Override
    public Room getRoomByRid(Integer rid) throws Exception{
        //通过方法查询房源
        Room room = roomDao.getRoomByRid(rid);
        //请求User接口差查询User对象set到room
        room.setUser((User) JsonTools.string2Obj(userService.getUserInfoByUid(room.getUserId()), ResultVo.class).getData());
        imgService.getRoomImgUrlByRid(room.getRoomId());
        return room;
    }

    /**
     * 根据城市Id查询房源
     * @param cid
     * @return  List<Room>
     */
    @Override
    public ResultVo selectRoomByCityId(Integer cid) {
        List<Room> rooms = roomDao.selectRoomByCityId(cid);
        for (int i = 0; i < rooms.size() ; i++) {
            rooms.get(i).setUser(RoomUtils.stringToObj(userService.getUserInfoByUid(rooms.get(i).getUserId()), User.class));
//            List<String> imgList = RoomUtils.StringToList(imgService.getRoomImgUrlByRid(rooms.get(i).getRoomId()), String.class);
            rooms.get(i).setImgList(RoomUtils.StringToList(imgService.getRoomImgUrlByRid(rooms.get(i).getRoomId()), String.class));
        }
        return ResultVo.getSuccessResultVo("查询成功",rooms);
    }


    /**
     * 根据传来的条件查询
     * @param cid  城市ID
     * @param pcount     房源可住几人
     * @param maxPrice         最大价
     * @param minPrice          最小价
     * @param leaseType        （'整租'或'单间出租'）
     * @param hid               房源类型(没用Fegin 直接在Mapper文件通过表查询)
     * @param grade             评分
     * @param start             分页开始数(需自己控制)
     * @param end               分页结束数
     * @return
     */
    @Override
    public ResultVo selectRoomByCondition(Integer cid, Integer pcount, Double maxPrice, Double minPrice, String leaseType, Integer hid, Double grade, Integer start, Integer end) {
        start = start >=0?start:0;
        start = start * end;
        List<Room> rooms = roomDao.selectRoomByCondition(cid, pcount, maxPrice, minPrice, leaseType, hid, grade, start, end);
        for (int i = 0; i < rooms.size(); i++) {
            rooms.get(i).setUser(RoomUtils.stringToObj(userService.getUserInfoByUid(rooms.get(i).getUserId()), User.class));
            rooms.get(i).setImgList(RoomUtils.StringToList(imgService.getRoomImgUrlByRid(rooms.get(i).getRoomId()), String.class));
        }
        return ResultVo.getSuccessResultVo("查询成功",rooms);
    }
}
